/*
 *  Copyright (c) M2TK Project. All rights reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package m2tk.assistant.app.ui.component;

import com.google.common.eventbus.EventBus;
import m2tk.assistant.app.ui.event.ShowStreamInfoPaneRequest;
import m2tk.assistant.app.ui.view.StreamInfoView;
import m2tk.assistant.common.ui.RoundPanel;
import net.miginfocom.swing.MigLayout;
import org.kordamp.ikonli.fluentui.FluentUiRegularAL;
import org.kordamp.ikonli.swing.FontIcon;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

import static m2tk.assistant.common.ui.UIConstants.ICON_SIZE_S;
import static m2tk.assistant.common.ui.UIConstants.LIGHT_BLUE;

public class StreamInfoNaviPane extends RoundPanel
{
    private ButtonGroup buttonGroup;
    private EventBus eventBus;

    public static final Icon BOARD_ICON = FontIcon.of(FluentUiRegularAL.BOARD_24, ICON_SIZE_S, LIGHT_BLUE);

    public StreamInfoNaviPane()
    {
        initUI();
    }

    private void initUI()
    {
        setLayout(new MigLayout("insets 10", "[grow]", "[25!][pref!][][grow]"));

        JLabel title = new JLabel();
        title.setText("传输流分析");
        title.setIcon(FontIcon.of(FluentUiRegularAL.GLANCE_24, ICON_SIZE_S, LIGHT_BLUE));
        title.setIconTextGap(5);
        add(title, "align left, wrap");
        add(separator(), "grow, wrap");

        buttonGroup = new ButtonGroup();

        Box board = Box.createVerticalBox();
        board.add(Box.createVerticalStrut(5));
        board.add(createNaviButton(buttonGroup,
                                   "传输流概览",
                                   e -> showStreamInfoPane(StreamInfoView.TRANSPORT_STREAM_SUMMARY_PANE)));
        board.add(Box.createVerticalStrut(5));
        board.add(createNaviButton(buttonGroup,
                                   "基本流概览",
                                   e -> showStreamInfoPane(StreamInfoView.ELEMENTARY_STREAM_SUMMARY_PANE)));
        board.add(Box.createVerticalStrut(5));
        board.add(createNaviButton(buttonGroup,
                                   "MPEG节目",
                                   e -> showStreamInfoPane(StreamInfoView.MPEG_PROGRAM_INFO_PANE)));
        board.add(Box.createVerticalStrut(5));
        board.add(createNaviButton(buttonGroup,
                                   "条件接收系统",
                                   e -> showStreamInfoPane(StreamInfoView.CA_SYSTEM_INFO_PANE)));
        board.add(Box.createVerticalStrut(5));
        board.add(createNaviButton(buttonGroup,
                                   "网络结构",
                                   e -> showStreamInfoPane(StreamInfoView.NETWORK_INFO_PANE)));
        board.add(Box.createVerticalStrut(5));
        board.add(createNaviButton(buttonGroup,
                                   "DVB业务",
                                   e -> showStreamInfoPane(StreamInfoView.DVB_SERVICE_INFO_PANE)));

        add(board, "span 1 2, grow");

        buttonGroup.clearSelection();
    }

    private JToggleButton createNaviButton(ButtonGroup group, String text, ActionListener action)
    {
        JToggleButton button = new JToggleButton();
        button.setText(text);
        button.setIcon(BOARD_ICON);
        button.addActionListener(action);

        button.setHorizontalAlignment(SwingConstants.LEADING);
        button.setAlignmentX(Component.CENTER_ALIGNMENT);
        button.putClientProperty("FlatLaf.style",
                                 """
                                 borderWidth: 0; focusWidth: 0; innerFocusWidth: 0; innerOutlineWidth: 0;
                                 background: #232227;
                                 hoverBackground: #18181A; selectedBackground: $List.selectionBackground;
                                 iconTextGap: 10;
                                 minimumWidth:200; minimumHeight: 40;
                                 """);

        group.add(button);

        return button;
    }

    public void setEventBus(EventBus bus)
    {
        eventBus = bus;
    }

    public void triggerFirstIfNoneSelected()
    {
        if (buttonGroup.getSelection() == null)
        {
            buttonGroup.getElements()
                       .nextElement()
                       .doClick(0);
        }
    }

    private void showStreamInfoPane(String type)
    {
        EventBus bus = eventBus;
        if (bus != null)
        {
            bus.post(new ShowStreamInfoPaneRequest(type));
        }
    }
}
